package top.jamsee.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;
import top.jamsee.dao.BuyDao;
import top.jamsee.entity.Buy_data;
import top.jamsee.entity.FileInfo;
import top.jamsee.entity.ResultInfo;
import top.jamsee.service.BuyService;
import top.jamsee.util.fileUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BuyServiceImpl implements BuyService {
    @Resource
    private BuyDao dao;

    @Override
    //查询买房表的数据
    public List<List> SelectBuyData(int pageNum) {


        //当前页数
//        System.out.println(p.getPageNum());
        //获取全部页数
        //每页的大小
//        System.out.println(p.getPageSize());
        //总页数
        Page p=PageHelper.startPage(pageNum,20);
        List<Buy_data> data=dao.selectBuyData();
        //算出多少页=全部多少行,每页多少条
        int colunms=dao.selectBuyNum();
        int pages=colunms/20;

        List<List> ls=new ArrayList<List>();
        List<Integer> pageList=new ArrayList<Integer>();
        pageList.add(pages);
        ls.add(data);
        ls.add(pageList);




        //返回

        return ls;
    }

    //删除买房表的数据
    @Override
    public int deleteBuyData(int id){
         int res=dao.deleteBuyDataById(id);//
        System.out.println("结果是"+res);
        return res;
    }

    @Override
    public int selectBuyNum() {
        return dao.selectBuyNum();
    }

    //添加一条记录(需要操作两个表 需要事务)
    @Override
    public ResultInfo addBuyData(Buy_data buy_data, HttpServletRequest request){
        String jsonFileAddress="";
        //there is fileupload
        String fileName= null;
        String jsonFileMessage="";
        try {
            fileName = fileUpload.uploadFile(request);
        } catch (Exception e) {
            e.printStackTrace();
            //插入失败
            return new ResultInfo(false,"文件上传失败");

        }
        System.out.println("文件上传成功");
        System.out.println(fileName);
         //文件上传发生异常时
         if ("none".equals(fileName)){
             return new ResultInfo(false,"文件上传失败");
         }
         //没有上传文件时
        if ("".equals(fileName)){
            return new ResultInfo(false,"请添加文件进行上传");
        }



        ObjectMapper om=new ObjectMapper();
         //文件的json信息,考虑到后面要上传多个文件
        System.out.println("file fail");
        FileInfo fl=new FileInfo();
        System.out.println("file fail1");
        ArrayList sl=new ArrayList();
        System.out.println("file fail2");
        sl.add(0,fileName);
        System.out.println("file fail3");
        fl.setFileName(sl);
        System.out.println("file fail4");
        try {
            System.out.println("json fail");
            jsonFileMessage=om.writeValueAsString(fl);

        } catch (JsonProcessingException e) {
            System.out.println("json fail");
            e.printStackTrace();
        }
        //get filename
        buy_data.setImgAddress(jsonFileMessage);

        int addRes=dao.addBuyData(buy_data);
        //there


        return new  ResultInfo(true,"成功插入数据");
    }

    @Override
    //模糊查询buy表
    public List selectBuyByData(int pageNum,String data) {


        //当前页数
//        System.out.println(p.getPageNum());
        //获取全部页数
        //每页的大小
//        System.out.println(p.getPageSize());
        //总页数
        Page p=PageHelper.startPage(pageNum,20);
        List<Buy_data> buy_data=dao.selectBuyByData(data);
        //算出多少页=全部多少行,每页多少条
        int colunms=dao.selectBuyByDataNum(data);
        int pages=colunms;

        List ls=new ArrayList();
        ls.add(buy_data);
        ls.add(pages);



        //返回
        return ls;


    }
    //模糊查询buy表的所有数量方便后续分页
    @Override
    public int selectBuyByDataNum(String data ){

            return dao.selectBuyByDataNum(data);
    }
    //更新buy表根据id
    @Override
    public ResultInfo upDateBuyById(Buy_data buy_data, HttpServletRequest request){
        String jsonFileAddress="";
        //there is fileupload
        String fileName= null;
        String jsonFileMessage="";
        try {
            fileName = fileUpload.uploadFile(request);
        } catch (Exception e) {
            e.printStackTrace();
            //插入失败
            return new ResultInfo(false,"文件更新失败");

        }
        System.out.println("文件上传成功");
        System.out.println(fileName);
        //文件上传发生异常时
        if ("none".equals(fileName)){
            return new ResultInfo(false,"文件更新失败");
        }
        //没有上传文件时
        if ("".equals(fileName)){
            return new ResultInfo(false,"请添加文件进行更新");
        }



        ObjectMapper om=new ObjectMapper();
        //文件的json信息,考虑到后面要上传多个文件
        FileInfo fl=new FileInfo();
        ArrayList sl=new ArrayList();
        sl.add(0,fileName);
        fl.setFileName(sl);

        try {
            jsonFileMessage=om.writeValueAsString(fl);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //get filename
        buy_data.setImgAddress(jsonFileMessage);

        int addRes=dao.upDateBuyById(buy_data);
        //there


        return new  ResultInfo(true,"成功更新数据");

    }
    //根据id查询是否有此条记录,更新表时使用
    @Override
    public int selectBuyById(int id){ return dao.selectBuyById(id); }


}
